﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKCopyModel.Basic;
using YJKCopyModel.CommonTrans;
using YJKCopyModel.Forms;
using YJKCopyModel.InteractiveOperation;
using YJKCopyModel.Models;

namespace YJKCopyModel.LastFastModeling
{
	public class LastFastModelingMaker : InteractiveOperationBase
	{
		public string Speciatly
		{
			get
			{
				return this.m_Speciatly;
			}
			set
			{
				this.m_Speciatly = value;
			}
		}

		public string EquipType
		{
			get
			{
				return this.m_EquipType;
			}
			set
			{
				this.m_EquipType = value;
			}
		}

		public LastFastModelingMaker(ExternalCommandData useExternalCommandData) : base(useExternalCommandData)
		{
			this.useuiDoc = useExternalCommandData.Application.ActiveUIDocument;
		}

		public override List<InputRequest> GetAllInputRequest()
		{
			return this.m_lstRequestes;
		}

		public override bool IfBreak
		{
			get
			{
				return this.m_bIfBreak;
			}
		}

		public override bool IfEndCommand
		{
			get
			{
				return this.m_ifEndCommand;
			}
		}

		public override void CloseFrom()
		{
			try
			{
				bool flag = this.m_Frm != null;
				if (flag)
				{
					this.m_Frm.Close();
				}
			}
			catch
			{
			}
		}

		public TerminalFastModelingForm CreateInstrance(Autodesk.Revit.DB.Document doc, string speciatly, string equipType)
		{
			bool flag = this.m_Frm == null;
			if (flag)
			{
				this.m_Frm = new TerminalFastModelingForm(doc, speciatly, equipType);
			}
			return this.m_Frm;
		}

		public override void PrepareRequest()
		{
			this.m_bIfBreak = true;
			this.m_bCancel = false;
			this.m_ifEndCommand = false;
			this.m_lstRequestes = new List<InputRequest>();
			Document document = this.useuiDoc.Document;
			bool flag = this.m_Frm == null;
			if (flag)
			{
				this.m_Frm = new TerminalFastModelingForm(document, this.m_Speciatly, this.m_EquipType);
				this.m_Frm.Show(WindowHelper.HWndRevit);
			}
			WindowHelper.SendMessageToWndRevit();
			bool flag2 = this.m_Frm != null;
			if (flag2)
			{
				this.m_Frm.IsLeaveDlg = false;
			}
			List<ICadElement> list = new List<ICadElement>();
			QuickModelingPara viewParas = this.m_Frm.GetViewParas();
			string publishGuid = this.m_Frm.PublishGuid;
			bool flag3 = viewParas == null || viewParas.IsWholeLevel;
			if (flag3)
			{
				bool flag4 = false;
				while (!flag4)
				{
					try
					{
						bool flag5 = !this.SelectBlocksByEquipType(viewParas, out this.lstTerminalBlockInfo);
						if (!flag5)
						{
							list.AddRange(this.lstTerminalBlockInfo);
							break;
						}
						flag4 = false;
						bool flag6 = false;
						string resultInfo = BussinessCommon.GetResultInfo(this.m_dwgEntitiesSelection, out flag6);
						bool flag7 = flag6;
						if (flag7)
						{
							this.m_ifEndCommand = true;
							this.m_bCancel = true;
							return;
						}
						YJKMessageBox.Show(resultInfo);
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
					{
						bool isLeaveDlg = this.m_Frm.IsLeaveDlg;
						if (isLeaveDlg)
						{
							this.m_bIfBreak = false;
						}
						throw ex;
					}
				}
			}
			else
			{
				bool isSelectBlock = this.m_IsSelectBlock;
				if (isSelectBlock)
				{
					bool flag8 = false;
					while (!flag8)
					{
						try
						{
							bool flag9 = !this.SelectBlocksByEquipType(viewParas, out this.lstTerminalBlockInfo);
							if (!flag9)
							{
								this.m_IsSelectBlock = false;
								break;
							}
							flag8 = false;
							bool flag10 = false;
							string resultInfo2 = BussinessCommon.GetResultInfo(this.m_dwgEntitiesSelection, out flag10);
							bool flag11 = flag10;
							if (flag11)
							{
								this.m_ifEndCommand = true;
								this.m_bCancel = true;
								return;
							}
							YJKMessageBox.Show(resultInfo2);
						}
						catch (Autodesk.Revit.Exceptions.OperationCanceledException ex2)
						{
							this.m_IsSelectBlock = true;
							bool isLeaveDlg2 = this.m_Frm.IsLeaveDlg;
							if (isLeaveDlg2)
							{
								this.m_bIfBreak = false;
							}
							throw ex2;
						}
					}
				}
				List<ICadElement> list2 = null;
				try
				{
					this.m_dwgEntitiesSelection.TryGetEntitesInPickBox<ICadElement>(out list2, this.lstTerminalBlockInfo, new DelegateInRange<ICadElement>(this.CheckBlockIsInRange), "请框选需要转换的区域!");
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex3)
				{
					this.m_IsSelectBlock = true;
					this.m_bIfBreak = false;
					throw ex3;
				}
				catch
				{
				}
				bool flag12 = list2 != null && list2.Count > 0;
				if (flag12)
				{
					list.AddRange(list2);
				}
			}
			viewParas = this.m_Frm.GetViewParas();
			bool flag13 = publishGuid != this.m_Frm.PublishGuid;
			if (flag13)
			{
				this.m_IsSelectBlock = true;
				this.m_bCancel = true;
			}
			else
			{
				FamilySymbol placeFamilySymbol = this.GetPlaceFamilySymbol(document, viewParas);
				bool flag14 = placeFamilySymbol == null;
				if (flag14)
				{
					YJKMessageBox.Show("获取族类型失败！请检查是否配置族文件或选择族类型。");
					this.m_IsSelectBlock = true;
					this.m_bCancel = true;
				}
				else
				{
					List<ICadElement> list3 = new List<ICadElement>();
					foreach (ICadElement item in list)
					{
						list3.Add(item);
					}
					LastFastModelingInputRequest lastFastModelingInputRequest = new LastFastModelingInputRequest();
					lastFastModelingInputRequest.SelectDataMap.Add("SelectedBlocks", list3);
					lastFastModelingInputRequest.ViewParas = viewParas;
					lastFastModelingInputRequest.PlaceFs = placeFamilySymbol;
					lastFastModelingInputRequest.m_Doc = document;
					lastFastModelingInputRequest.CurveMultiple = 10.0;
					lastFastModelingInputRequest.IsAddLabel = this.CheckIsNeedLabel(viewParas);
					this.m_lstRequestes.Add(lastFastModelingInputRequest);
				}
			}
		}

		private bool SelectBlocksByEquipType(QuickModelingPara viewParas, out List<ICadElement> lstTerminalBlockInfo)
		{
			lstTerminalBlockInfo = new List<ICadElement>();
			string equipType = BussinessCommon.GetEquipType(viewParas);
			string strPickGuidInfo = "请选择需要转换的图块！";
			bool flag = equipType == "暖通@风口";
			if (flag)
			{
				List<PointPosBlockWindMouthInfo> list = null;
				bool flag2 = this.m_dwgEntitiesSelection.TryPickSameNameBlockRef<PointPosBlockWindMouthInfo>(out list, false, strPickGuidInfo, "", false);
				bool flag3 = !list.IsEmpty<PointPosBlockWindMouthInfo>();
				if (flag3)
				{
					lstTerminalBlockInfo.AddRange(list);
				}
			}
			else
			{
				bool flag4 = equipType == "暖通@风机盘管";
				if (flag4)
				{
					DraughtFanPipePara draughtFanPipePara = viewParas.ChildViewPara as DraughtFanPipePara;
					bool flag5 = draughtFanPipePara == null;
					if (flag5)
					{
						return false;
					}
					bool isSelectBlock = draughtFanPipePara.IsSelectBlock;
					if (isSelectBlock)
					{
						List<PointPosBlockDuctCoilInfo> list2 = null;
						bool flag2 = this.m_dwgEntitiesSelection.TryPickSameNameBlockRef<PointPosBlockDuctCoilInfo>(out list2, false, strPickGuidInfo, "", true);
						bool flag6 = !list2.IsEmpty<PointPosBlockDuctCoilInfo>();
						if (flag6)
						{
							lstTerminalBlockInfo.AddRange(list2);
						}
					}
					else
					{
						strPickGuidInfo = "请选择风机盘管图层上的线！";
						SelecteEntType type = SelecteEntType.eLine | SelecteEntType.eArc | SelecteEntType.ePolyLine;
						List<CurveInfo> list3 = null;
						bool flag2 = this.m_dwgEntitiesSelection.TryPickSameLayerAllCurve(out list3, false, type, strPickGuidInfo);
						bool flag7 = !list3.IsEmpty<CurveInfo>();
						if (flag7)
						{
							lstTerminalBlockInfo.AddRange(list3);
						}
					}
				}
				else
				{
					bool flag8 = equipType == "暖通@室内机";
					if (flag8)
					{
						InDoorUnitPara inDoorUnitPara = viewParas.ChildViewPara as InDoorUnitPara;
						bool flag9 = inDoorUnitPara == null;
						if (flag9)
						{
							return false;
						}
						bool isSelectBlock2 = inDoorUnitPara.IsSelectBlock;
						if (isSelectBlock2)
						{
							List<PointPosBlockDuctCoilInfo> list4 = null;
							bool flag2 = this.m_dwgEntitiesSelection.TryPickSameNameBlockRef<PointPosBlockDuctCoilInfo>(out list4, false, strPickGuidInfo, "", true);
							bool flag10 = !list4.IsEmpty<PointPosBlockDuctCoilInfo>();
							if (flag10)
							{
								lstTerminalBlockInfo.AddRange(list4);
							}
						}
						else
						{
							strPickGuidInfo = "请选择室内机图层上的线！";
							SelecteEntType type2 = SelecteEntType.eLine | SelecteEntType.eArc | SelecteEntType.ePolyLine;
							List<CurveInfo> list5 = null;
							bool flag2 = this.m_dwgEntitiesSelection.TryPickSameLayerAllCurve(out list5, false, type2, strPickGuidInfo);
							bool flag11 = !list5.IsEmpty<CurveInfo>();
							if (flag11)
							{
								lstTerminalBlockInfo.AddRange(list5);
							}
						}
					}
					else
					{
						bool flag12 = equipType == "暖通@散热器";
						if (flag12)
						{
							RadiatorPara radiatorPara = viewParas.ChildViewPara as RadiatorPara;
							bool flag13 = radiatorPara == null;
							if (flag13)
							{
								return false;
							}
							bool isSelectBlock3 = radiatorPara.IsSelectBlock;
							if (isSelectBlock3)
							{
								List<PointPosBlockDuctCoilInfo> list6 = null;
								bool flag2 = this.m_dwgEntitiesSelection.TryPickSameNameBlockRef<PointPosBlockDuctCoilInfo>(out list6, false, strPickGuidInfo, "", false);
								bool flag14 = !list6.IsEmpty<PointPosBlockDuctCoilInfo>();
								if (flag14)
								{
									lstTerminalBlockInfo.AddRange(list6);
								}
							}
							else
							{
								strPickGuidInfo = "请选择散热器图层上的线！";
								SelecteEntType type3 = SelecteEntType.eLine | SelecteEntType.eArc | SelecteEntType.ePolyLine;
								List<CurveInfo> list7 = null;
								bool flag2 = this.m_dwgEntitiesSelection.TryPickSameLayerAllCurve(out list7, false, type3, strPickGuidInfo);
								bool flag15 = !list7.IsEmpty<CurveInfo>();
								if (flag15)
								{
									lstTerminalBlockInfo.AddRange(list7);
								}
							}
						}
						else
						{
							List<PointPosBlockInfo> list8 = null;
							bool flag2 = this.m_dwgEntitiesSelection.TryPickSameNameBlockRef<PointPosBlockInfo>(out list8, false, strPickGuidInfo, "", false);
							bool flag16 = !list8.IsEmpty<PointPosBlockInfo>();
							if (flag16)
							{
								lstTerminalBlockInfo.AddRange(list8);
							}
						}
					}
				}
			}
			bool flag17 = lstTerminalBlockInfo.IsEmpty<ICadElement>();
			return !flag17;
		}

		public bool CheckBlockIsInRange(ICadElement blockRef, PickedBox pickBox)
		{
			bool flag = blockRef == null || pickBox == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = blockRef is PointPosBlockInfo;
				if (flag2)
				{
					PointPosBlockInfo pointPosBlockInfo = blockRef as PointPosBlockInfo;
					XYZ useLocationPoint = pointPosBlockInfo.UseLocationPoint;
					bool flag3 = !BussinessCommon.GetBlockCenter(pointPosBlockInfo, out useLocationPoint);
					result = (!flag3 && RevitCommonFunctions.CheckPointIsInBox(pickBox, useLocationPoint));
				}
				else
				{
					bool flag4 = blockRef is CurveInfo;
					if (flag4)
					{
						Curve thisCurve = (blockRef as CurveInfo).ThisCurve;
						List<XYZ> curveEndPoints = RevitCommonFunctions.GetCurveEndPoints(thisCurve);
						bool flag5 = curveEndPoints == null || curveEndPoints.Count != 2;
						result = (!flag5 && RevitCommonFunctions.CheckPointIsInBox(pickBox, curveEndPoints[0]) && RevitCommonFunctions.CheckPointIsInBox(pickBox, curveEndPoints[1]));
					}
					else
					{
						result = false;
					}
				}
			}
			return result;
		}

		private bool CheckIsNeedLabel(QuickModelingPara paras)
		{
			bool flag = paras == null || string.IsNullOrEmpty(paras.SpecialtyName) || paras.SelectedEquipData == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				string a = paras.SpecialtyName + "@" + paras.SelectedEquipData.Name;
				bool flag2 = a == "给排水@灭火器";
				if (flag2)
				{
					HydrantViewPara hydrantViewPara = paras.ChildViewPara as HydrantViewPara;
					bool flag3 = hydrantViewPara != null && hydrantViewPara.IsAddLabel;
					if (flag3)
					{
						return true;
					}
				}
				result = false;
			}
			return result;
		}

		private FamilySymbol GetPlaceFamilySymbol(Autodesk.Revit.DB.Document doc, QuickModelingPara paras)
		{
			bool flag = paras == null || string.IsNullOrEmpty(paras.SpecialtyName) || paras.SelectedEquipData == null;
			FamilySymbol result;
			if (flag)
			{
				result = null;
			}
			else
			{
				string equipType = BussinessCommon.GetEquipType(paras);
				bool flag2 = equipType == "给排水@消火栓" || equipType == "给排水@灭火器";
				if (flag2)
				{
					HydrantViewPara hydrantViewPara = paras.ChildViewPara as HydrantViewPara;
					bool flag3 = hydrantViewPara == null;
					if (flag3)
					{
						result = null;
					}
					else
					{
						bool isCurDoc = hydrantViewPara.IsCurDoc;
						if (isCurDoc)
						{
							result = hydrantViewPara.SelectedFs;
						}
						else
						{
							result = this.GetFamilySymbol(doc, paras.SelectedEquipData.TableName, hydrantViewPara.FamilyId, hydrantViewPara.SelectedModel);
						}
					}
				}
				else
				{
					bool flag4 = equipType == "暖通@风口";
					if (flag4)
					{
						WindMouthPara windMouthPara = paras.ChildViewPara as WindMouthPara;
						bool flag5 = windMouthPara == null;
						if (flag5)
						{
							result = null;
						}
						else
						{
							bool isCurDoc2 = windMouthPara.IsCurDoc;
							if (isCurDoc2)
							{
								result = windMouthPara.SelectedFs;
							}
							else
							{
								result = this.GetFamilySymbol(doc, paras.SelectedEquipData.TableName, windMouthPara.FamilyId, windMouthPara.SelectedModel);
							}
						}
					}
					else
					{
						bool flag6 = equipType == "暖通@风机盘管";
						if (flag6)
						{
							DraughtFanPipePara draughtFanPipePara = paras.ChildViewPara as DraughtFanPipePara;
							bool flag7 = draughtFanPipePara == null;
							if (flag7)
							{
								result = null;
							}
							else
							{
								bool isCurDoc3 = draughtFanPipePara.IsCurDoc;
								if (isCurDoc3)
								{
									result = draughtFanPipePara.SelectedFs;
								}
								else
								{
									result = this.GetWindMouthFamilySymbol(doc, draughtFanPipePara.FamilyId);
								}
							}
						}
						else
						{
							bool flag8 = equipType == "暖通@室内机";
							if (flag8)
							{
								InDoorUnitPara inDoorUnitPara = paras.ChildViewPara as InDoorUnitPara;
								bool flag9 = inDoorUnitPara == null;
								if (flag9)
								{
									result = null;
								}
								else
								{
									bool isCurDoc4 = inDoorUnitPara.IsCurDoc;
									if (isCurDoc4)
									{
										result = inDoorUnitPara.SelectedFs;
									}
									else
									{
										result = LastFastModelingMaker.GetInDoorUnitFamilySymbol(doc, inDoorUnitPara.SelectedFamilyName);
									}
								}
							}
							else
							{
								bool flag10 = equipType == "暖通@散热器";
								if (flag10)
								{
									RadiatorPara radiatorPara = paras.ChildViewPara as RadiatorPara;
									bool flag11 = radiatorPara == null;
									if (flag11)
									{
										result = null;
									}
									else
									{
										bool isCurDoc5 = radiatorPara.IsCurDoc;
										if (isCurDoc5)
										{
											result = radiatorPara.SelectedFs;
										}
										else
										{
											result = LastFastModelingMaker.GetFamilySymbol(doc, paras.SelectedEquipData.TableName, radiatorPara.SelectedFamilyName, radiatorPara.SelectedModel);
										}
									}
								}
								else
								{
									ElectricViewPara electricViewPara = paras.ChildViewPara as ElectricViewPara;
									bool flag12 = electricViewPara == null;
									if (flag12)
									{
										result = null;
									}
									else
									{
										bool isCurDoc6 = electricViewPara.IsCurDoc;
										if (isCurDoc6)
										{
											result = electricViewPara.SelectedFs;
										}
										else
										{
											result = this.GetFamilySymbol(doc, paras.SelectedEquipData.TableName, electricViewPara.FamilyId, electricViewPara.SelectedModel);
										}
									}
								}
							}
						}
					}
				}
			}
			return result;
		}

		private FamilySymbol GetFamilySymbol(Autodesk.Revit.DB.Document doc, string equipTable, int familyId, string familySymbolName)
		{
			return this.m_Sfdr.GetFamilySymbolById(familyId, equipTable, familySymbolName, doc);
		}

		private FamilySymbol GetWindMouthFamilySymbol(Autodesk.Revit.DB.Document doc, int recId)
		{
			return this.m_Sfdr.GetFamilySymbolById(recId, doc, BuiltInCategory.OST_MechanicalEquipment);
		}

		private static FamilySymbol GetInDoorUnitFamilySymbol(Autodesk.Revit.DB.Document doc, string FamilyName)
		{
			FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(FamilyName, doc);
			bool flag = familySymbol != null;
			FamilySymbol result;
			if (flag)
			{
				result = familySymbol;
			}
			else
			{
				string text = "IndoorUnit";
				string path = string.Concat(new string[]
				{
					"VRV\\",
					text,
					"\\",
					FamilyName,
					".rfa"
				});
				string text2 = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), path);
				bool flag2 = !File.Exists(text2);
				if (flag2)
				{
					result = familySymbol;
				}
				else
				{
					Transaction transaction = new Transaction(doc, "Load");
					try
					{
						transaction.Start();
						doc.LoadFamily(text2);
					}
					catch
					{
						transaction.RollBack();
						return null;
					}
					finally
					{
						bool flag3 = (int)transaction.GetStatus() == 1;
						if (flag3)
						{
							transaction.Commit();
						}
					}
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol));
					foreach (Element element in filteredElementCollector)
					{
						bool flag4 = (element as FamilySymbol).Family.Name == FamilyName;
						if (flag4)
						{
							familySymbol = (element as FamilySymbol);
							break;
						}
					}
					bool flag5 = familySymbol == null;
					if (flag5)
					{
						result = null;
					}
					else
					{
						result = familySymbol;
					}
				}
			}
			return result;
		}

		private static FamilySymbol GetFamilySymbol(Autodesk.Revit.DB.Document doc, string strType, string familyName, string familySymbolName)
		{
			FamilySymbol docFamilySymbol = LastFastModelingMaker.GetDocFamilySymbol(doc, familyName, familySymbolName);
			bool flag = docFamilySymbol != null;
			FamilySymbol result;
			if (flag)
			{
				result = docFamilySymbol;
			}
			else
			{
				string path = strType + "\\" + familyName + ".rfa";
				string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), path);
				bool flag2 = !File.Exists(text);
				if (flag2)
				{
					result = docFamilySymbol;
				}
				else
				{
					Transaction transaction = new Transaction(doc, "Load");
					try
					{
						transaction.Start();
						doc.LoadFamily(text);
					}
					catch
					{
						transaction.RollBack();
						return null;
					}
					finally
					{
						bool flag3 = (int)transaction.GetStatus() == 1;
						if (flag3)
						{
							transaction.Commit();
						}
					}
					docFamilySymbol = LastFastModelingMaker.GetDocFamilySymbol(doc, familyName, familySymbolName);
					result = docFamilySymbol;
				}
			}
			return result;
		}

		public static FamilySymbol GetDocFamilySymbol(Autodesk.Revit.DB.Document doc, string familyName, string familySymbolName)
		{
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(FamilySymbol), doc);
			FamilySymbol familySymbol = null;
			bool flag = elementByClass.Count <= 0;
			FamilySymbol result;
			if (flag)
			{
				result = null;
			}
			else
			{
				foreach (Element element in elementByClass)
				{
					FamilySymbol familySymbol2 = element as FamilySymbol;
					bool flag2 = familySymbol2 == null;
					if (!flag2)
					{
						bool flag3 = familySymbol2.Name == familySymbolName && familySymbol2.Family.Name == familyName;
						if (flag3)
						{
							familySymbol = familySymbol2;
							break;
						}
					}
				}
				result = familySymbol;
			}
			return result;
		}

		private List<InputRequest> m_lstRequestes = new List<InputRequest>();

		private UIDocument useuiDoc;

		private bool m_bIfBreak = true;

		private bool m_ifEndCommand = false;

		private string m_Speciatly;

		private string m_EquipType;

		private SpaceFamilyDataReader m_Sfdr = new SpaceFamilyDataReader();

		private TerminalFastModelingForm m_Frm = null;

		private bool m_IsSelectBlock = true;

		private List<ICadElement> lstTerminalBlockInfo = null;
	}
}
